<template>
  <div class="home-container">
    <!-- 1. 底层：原始背景图 -->
    <div class="background-original"></div>

    <!-- 2. 中层：裁剪后的背景图（遮挡底层的草） -->
    <div class="background-masked"></div>

    <!-- 花瓣飘落容器 -->
    <div 
      class="petal-container" 
      contain="layout paint"
      v-show="isPageVisible"
    >
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
      <div class="petal"></div>
    </div>
    
    <div class="content">
      <!-- 导航栏组件 -->
      <Navbar />

      <!-- Hero 区域 -->
      <section class="hero">
        <div class="hero-content">
          <h2 class="hero-title">以创意为笔，AI 为幕</h2>
          <p class="hero-subtitle">点亮你的艺术想象</p>
          <p class="hero-desc">
            无需专业技法，仅需一段文字描述，即可将抽象灵感转化为具象作品，让每一份构想都拥有独特的艺术形态。
          </p>
        </div>
      </section>

      <!-- 精选作品 -->
      <section class="gallery">
        <h2 class="section-title">精选作品</h2>
        
        <!-- AI绘画作品 -->
        <div class="category-section">
          <h3 class="category-title">AI视觉艺术</h3>
          <div class="gallery-grid">
            <div v-for="i in 4" :key="'painting-' + i" class="gallery-item">
              <div class="gallery-image">
                <div class="placeholder">🎨</div>
              </div>
              <div class="gallery-info">
                <h4>绘画作品 {{ i }}</h4>
                <div class="gallery-meta">
                  <span class="tag">{{ getPaintingStyleTag(i) }}</span>
                  <span class="creation-time">{{ getCreationDate(i) }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
        
        <!-- AI音乐作品 -->
        <div class="category-section">
          <h3 class="category-title">AI声音创作</h3>
          <div class="gallery-grid">
            <div v-for="i in 4" :key="'music-' + i" class="gallery-item">
              <div class="gallery-image">
                <div class="placeholder">🎵</div>
              </div>
              <div class="gallery-info">
                <h4>音乐作品 {{ i }}</h4>
                <div class="gallery-meta">
                  <span class="tag">{{ getMusicStyleTag(i) }}</span>
                  <span class="creation-time">{{ getCreationDate(i + 4) }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
        
        <!-- AI文学作品 -->
        <div class="category-section">
          <h3 class="category-title">AI文学创作</h3>
          <div class="gallery-grid">
            <div v-for="i in 4" :key="'literature-' + i" class="gallery-item">
              <div class="gallery-image">
                <div class="placeholder">📚</div>
              </div>
              <div class="gallery-info">
                <h4>文学作品 {{ i }}</h4>
                <div class="gallery-meta">
                  <span class="tag">{{ getLiteratureStyleTag(i) }}</span>
                  <span class="creation-time">{{ getCreationDate(i + 8) }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </section>

      <!-- 引导转化 -->
      <section class="conversion">
        <div class="conversion-content">
          <h3>灵感转瞬即逝，不妨此刻落笔</h3>
          <p>让你的文字想象，成为下一幅被看见的 AI 艺术。点击顶部「艺术创作」按钮，开启你的首次创作，所有作品均可存入个人作品集</p>
          <button class="conversion-btn">立即创作</button>
        </div>
      </section>

      <!-- 底部 -->
      <footer class="footer">
        <p>© 2025 AI 艺术创作平台 - 让创意无界，让 AI 赋能艺术创作</p>
      </footer>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onBeforeMount, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import Navbar from '../components/Navbar.vue'

// 风格标签数组
const paintingStyleTags = ['国潮风', '赛博朋克', '治愈系', '古典', '现代', '未来主义', '极简风', '抽象派']
const musicStyleTags = ['治愈系', '电子乐', '古典风', '流行', '氛围音乐', '摇滚', '爵士', '民谣']
const literatureStyleTags = ['古风叙事', '科幻悬疑', '现代诗歌', '散文随笔', '奇幻冒险', '都市情感', '历史传奇', '哲理思辨']

const router = useRouter()

// 背景图片加载状态
const isBackgroundLoaded = ref(false)

// 控制花瓣动画是否运行
const isPageVisible = ref(true)

// 监听页面可见性变化
const handleVisibilityChange = () => {
  isPageVisible.value = !document.hidden
}

onMounted(() => {
  document.addEventListener('visibilitychange', handleVisibilityChange)
})

onUnmounted(() => {
  document.removeEventListener('visibilitychange', handleVisibilityChange)
})

// 预加载背景图片
const preloadBackgroundImage = () => {
  return new Promise((resolve) => {
    const img = new Image();
    img.src = '/home-background.png';
    img.onload = () => {
      isBackgroundLoaded.value = true;
      resolve(true);
    };
    img.onerror = () => {
      console.error('背景图片加载失败');
      isBackgroundLoaded.value = true;
      resolve(false);
    };
  });
}

// 在组件挂载前预加载背景图片
onBeforeMount(async () => {
  await preloadBackgroundImage();
})

// 获取绘画风格标签
const getPaintingStyleTag = (index: number) => {
  return paintingStyleTags[(index - 1) % paintingStyleTags.length]
}

// 获取音乐风格标签
const getMusicStyleTag = (index: number) => {
  return musicStyleTags[(index - 1) % musicStyleTags.length]
}

// 获取文学风格标签
const getLiteratureStyleTag = (index: number) => {
  return literatureStyleTags[(index - 1) % literatureStyleTags.length]
}

// 获取创作日期
const getCreationDate = (index: number) => {
  // 生成最近几天的日期
  const date = new Date()
  date.setDate(date.getDate() - (index - 1))
  return date.toISOString().split('T')[0] // 格式化为 YYYY-MM-DD
}
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --petal-images: 
    url('/petal.png'),
    url('/petal1.png'),
    url('/petal2.png'); /* 多种花瓣图片 */
  --petal-sizes: 25px, 30px, 35px; /* 增大花瓣尺寸，提升视觉效果 */
  --petal-rotations: 0deg, 90deg, 180deg; /* 初始旋转角度 */
  --petal-delays: 0s, 1.5s, 3s; /* 调整动画延迟，避免过于集中 */
  --petal-durations: 12s, 18s, 15s; /* 调整动画时长，创造层次感 */
}

.home-container {
  position: relative;
  min-height: 100vh;
  width: 100vw;
  overflow: hidden; /* 确保内容不会溢出 */
}


.content {
  position: relative;
  z-index: 6;
  /* 确保内容可以正常滚动 */
  min-height: 100vh;
}

/* Hero 区域 */
.hero {
  width: 100vw;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  /* 移除overflow: hidden，允许内容滚动 */
}

.hero-content {
  position: relative;
  max-width: 800px;
  margin: 0 auto;
  text-align: center;
  color: white;
  padding: 0 24px;
  /* 添加一些padding确保内容不会紧贴屏幕边缘 */
  padding-top: 100px;
  padding-bottom: 100px;
}

.hero-title {
  font-size: 48px;
  font-weight: 700;
  margin-bottom: 16px;
  line-height: 1.2;
  color: #ffffff;
  text-shadow: 0 4px 12px rgba(0, 0, 0, 0.7), 0 0 20px rgba(0, 0, 0, 0.5);
}

.hero-subtitle {
  font-size: 24px;
  margin-bottom: 16px;
  color: #ffffff;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.6), 0 0 15px rgba(0, 0, 0, 0.4);
}

.hero-desc {
  font-size: 16px;
  margin-bottom: 40px;
  line-height: 1.6;
  color: #ffffff;
  text-shadow: 0 1px 6px rgba(0, 0, 0, 0.5), 0 0 10px rgba(0, 0, 0, 0.3);
  max-width: 600px;
}

/* 作品廊 */
.gallery {
  max-width: 100%;
  width: 100vw;
  margin: 80px auto;
  padding: 0 24px;
}

.category-section {
  margin-bottom: 60px;
}

.category-section:last-child {
  margin-bottom: 0;
}

.category-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin-bottom: 24px;
  padding-left: 12px;
  border-left: 4px solid #667eea;
}

.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 24px;
  max-width: 1200px;
  margin: 0 auto;
}

.gallery-item {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  cursor: pointer;
}

.gallery-item:hover {
  transform: scale(1.05);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.15);
}

.gallery-image {
  width: 100%;
  height: 200px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}

.placeholder {
  font-size: 48px;
  opacity: 0.5;
}

.gallery-info {
  padding: 16px;
}

.gallery-info h4 {
  font-size: 16px;
  color: #333;
  margin-bottom: 8px;
}

.gallery-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 12px;
}

.tag {
  font-size: 12px;
  padding: 4px 12px;
  background: #f0f0f0;
  color: #666;
  border-radius: 12px;
}

.creation-time {
  font-size: 12px;
  color: #999;
}

/* 引导转化 */
.conversion {
  max-width: 100%;
  width: 100vw;
  margin: 80px auto;
  padding: 0 24px;
  text-align: center;
}

.conversion-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 60px 40px;
}

.conversion-content h3 {
  font-size: 28px;
  margin-bottom: 16px;
  font-weight: 600;
  color: #333;
}

.conversion-content p {
  font-size: 16px;
  color: #666;
  margin-bottom: 32px;
  line-height: 1.6;
  max-width: 600px;
  margin-left: auto;
  margin-right: auto;
}

.conversion-btn {
  padding: 14px 32px;
  background: #667eea;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
}

.conversion-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3);
}

/* 底部 */
.footer {
  background: #333;
  color: white;
  text-align: center;
  padding: 40px 24px;
  margin-top: 80px;
  width: 100%;
}

.footer p {
  font-size: 14px;
  opacity: 0.8;
}

/* 响应式 */
@media (max-width: 768px) {
  .nav-links {
    gap: 16px;
  }

  .hero-title {
    font-size: 32px;
  }

  .hero-subtitle {
    font-size: 18px;
  }

  .section-title {
    font-size: 28px;
  }

  .conversion-content {
    padding: 40px 24px;
  }

  .conversion-content h3 {
    font-size: 24px;
  }
  
  .feature-grid,
  .gallery-grid {
    grid-template-columns: 1fr;
  }
  
  .hero-content {
    padding-top: 60px;
    padding-bottom: 60px;
  }
}

/* 花瓣容器 */
.petal-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none; /* 不阻挡鼠标事件 */
  z-index: 10; /* 确保花瓣在页面内容上方显示 */
  contain: layout paint; /* 限制布局和绘制范围 */
  /* 添加以下优化 */
  will-change: transform;
  transform: translateZ(0); /* 触发硬件加速 */
}

.petal {
  position: absolute;
  width: var(--size);
  height: var(--size);
  background-image: var(--image);
  background-size: contain;
  background-repeat: no-repeat;
  animation: fall var(--duration) linear infinite;
  opacity: 0;
  filter: drop-shadow(0 0 2px rgba(255, 255, 255, 0.3)); /* 仅保留轻微发光效果 */
  
  /* 优化动画性能 */
  will-change: transform, opacity;
  backface-visibility: hidden;
  perspective: 1000px;
  transform: translateZ(0); /* 触发硬件加速 */
}

/* 简化动画关键帧，减少关键帧数量 */
@keyframes fall {
  0% {
    transform: translateY(-100px) rotate(0deg) scale(0.8);
    opacity: 0;
  }
  10% {
    opacity: 0.8;
  }
  90% {
    opacity: 0.8;
  }
  100% {
    transform: translateY(100vh) rotate(360deg) scale(1.2);
    opacity: 0;
  }
}

/* 为不同花瓣设置更多样式变化，创造视觉多样性 */
.petal:nth-child(10n) {
  --image: url('/petal.png');
  --size: 25px;
  --rotation: 0deg;
  --duration: 12s;
  --end-opacity: 0.6;
  --scale: 1;
  left: 2%;
  opacity: 0.7;
  filter: drop-shadow(0 0 2px rgba(255, 255, 255, 0.3));
}

.petal:nth-child(10n+1) {
  --image: url('/petal1.png');
  --size: 30px;
  --rotation: 45deg;
  --duration: 18s;
  --end-opacity: 0.7;
  --scale: 1.1;
  left: 8%;
  opacity: 0.8;
  filter: drop-shadow(0 0 2px rgba(255, 255, 255, 0.3));
}

.petal:nth-child(10n+2) {
  --image: url('/petal2.png');
  --size: 35px;
  --rotation: 90deg;
  --duration: 15s;
  --end-opacity: 0.8;
  --scale: 1.2;
  left: 15%;
  opacity: 0.9;
  filter: drop-shadow(0 0 2px rgba(255, 255, 255, 0.3));
}

.petal:nth-child(10n+3) {
  --image: url('/petal.png');
  --size: 20px;
  --rotation: 135deg;
  --duration: 10s;
  --end-opacity: 0.5;
  --scale: 0.9;
  left: 25%;
  opacity: 0.6;
  filter: drop-shadow(0 0 2px rgba(255, 255, 255, 0.3));
}

.petal:nth-child(10n+4) {
  --image: url('/petal1.png');
  --size: 32px;
  --rotation: 180deg;
  --duration: 16s;
  --end-opacity: 0.75;
  --scale: 1.15;
  left: 35%;
  opacity: 0.85;
  filter: drop-shadow(0 0 2px rgba(255, 255, 255, 0.3));
}

.petal:nth-child(10n+5) {
  --image: url('/petal2.png');
  --size: 28px;
  --rotation: 225deg;
  --duration: 14s;
  --end-opacity: 0.65;
  --scale: 1.05;
  left: 45%;
  opacity: 0.75;
  filter: drop-shadow(0 0 2px rgba(255, 255, 255, 0.3));
}

.petal:nth-child(10n+6) {
  --image: url('/petal.png');
  --size: 22px;
  --rotation: 270deg;
  --duration: 13s;
  --end-opacity: 0.55;
  --scale: 0.95;
  left: 55%;
  opacity: 0.65;
  filter: drop-shadow(0 0 2px rgba(255, 255, 255, 0.3));
}

.petal:nth-child(10n+7) {
  --image: url('/petal1.png');
  --size: 34px;
  --rotation: 315deg;
  --duration: 19s;
  --end-opacity: 0.78;
  --scale: 1.25;
  left: 65%;
  opacity: 0.88;
  filter: drop-shadow(0 0 2px rgba(255, 255, 255, 0.3));
}

.petal:nth-child(10n+8) {
  --image: url('/petal2.png');
  --size: 26px;
  --rotation: 0deg;
  --duration: 11s;
  --end-opacity: 0.62;
  --scale: 1.02;
  left: 75%;
  opacity: 0.72;
  filter: drop-shadow(0 0 2px rgba(255, 255, 255, 0.3));
}

.petal:nth-child(10n+9) {
  --image: url('/petal.png');
  --size: 29px;
  --rotation: 60deg;
  --duration: 17s;
  --end-opacity: 0.68;
  --scale: 1.12;
  left: 85%;
  opacity: 0.78;
  filter: drop-shadow(0 0 2px rgba(255, 255, 255, 0.3));
}

/* 容器 */
.home-container {
  position: relative;
  min-height: 100vh;
  overflow: hidden; /* 确保内容不会溢出 */
}

/* 1. 底层：原始背景图 */
.background-original {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url('/home-background.png') center/cover no-repeat;
  z-index: -3; /* 最底层 */
}

/* 2. 中层：裁剪后的背景图（关键） */
.background-masked {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url('/home-background.png') center/cover no-repeat;
  z-index: -2; /* 在底层之上 */
}

</style>